import React, { Component } from 'react';
import { SearchBar } from 'antd-mobile';
import styles from './index.module.scss'
import { getCurrentCity } from 'utils/city.js'
import store from 'store/index'

class rentSearch extends Component {
    state = {
        list: null
    }
    componentDidMount() {
        this.getCurrentCity()
    }
    // 获取当前城市id
    getCurrentCity = async () => {
        let { value } = await getCurrentCity()
        this.value = value
    }
    onChange = val => {
        this.search(val)
    }
    search = async keyword => {
        const res = await this.axios.get("/area/community", {
            params: {
                name: keyword,
                id: this.value
            }
        })
        this.setState({
            list: res.data.body
        })
    }
    toggleSelect = ({community,communityName}) => {
        console.log(community,communityName)
        store.dispatch({
            type: 'SET_COMMUNITY',
            payload: { community, communityName }
        })

        this.props.history.goBack()
    }
    render() {
        const { list } = this.state
        return (
            <div>
                <SearchBar
                    placeholder="请输入小区和地址"
                    onChange={this.onChange}
                />
                <ul className={styles.tips}>
                    {list &&
                        list.map(item => {
                            return (
                                <li onClick={() => this.toggleSelect(item)} className={styles.tip} key={item.community}>
                                    {item.communityName}
                                </li>
                            )
                        })
                    }
                </ul>
            </div>
        );
    }
}

export default rentSearch;